package com.fruit.service.fruit;

import com.fruit.bean.dto.fruit.FruitCommentDto;
import com.fruit.bean.entity.fruit.FruitCommentInfo;
import com.fruit.bean.entity.system.User;
import com.fruit.bean.vo.query.SearchFilter;
import com.fruit.dao.fruit.FruitCommentRepository;
import com.fruit.dao.fruit.FruitUserOrderRepository;
import com.fruit.service.BaseService;
import com.fruit.service.system.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class FruitCommentService extends BaseService<FruitCommentInfo,Long, FruitCommentRepository> {

    @Autowired
    private FruitCommentRepository commentRepository;

    @Autowired
    private FruitUserOrderRepository orderRepository;

    @Autowired
    private UserService userService;

    /**
     * 小程序端获取商品评论列表
     * @param shelfId 商品id
     * @return
     */
    public List<FruitCommentDto> listFruitComment(Long shelfId){
        SearchFilter filter = SearchFilter.build("fruitShelfId", SearchFilter.Operator.EQ, shelfId);
        List<FruitCommentInfo> commentInfoList = this.queryAll(filter);
        List<FruitCommentDto> commentDtoList = new ArrayList<>();
        for (FruitCommentInfo fruitCommentInfo: commentInfoList){
            FruitCommentDto commentDto = new FruitCommentDto();
            commentDto.setCommentDate(fruitCommentInfo.getFruitCommentTime());
            commentDto.setCommentValue(fruitCommentInfo.getFruitCommentValue());
            User user = userService.get(fruitCommentInfo.getUserId());
            commentDto.setUserName(user.getAccount());
            commentDtoList.add(commentDto);
        }
        return commentDtoList;
    }
}
